package com.prb.netty.code;

import com.prb.netty.protocol.JsonProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class JsonEncoder extends MessageToByteEncoder<JsonProtocol> {

    @Override
    protected void encode(ChannelHandlerContext tcx, JsonProtocol jp,
                          ByteBuf out) throws Exception {
        // 写入消息的长度
        out.writeInt(jp.getContentLength());
        // 写入消息类容
        out.writeBytes(jp.getContent());
    }

}